<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | EnterpriseBpmsInstance.php v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018/11/23
// +-------------------------------------------------------------------------------

namespace app\core\model;


use think\Db;

class EnterpriseBpmsInstance {
    //审批实例状态
    const STATUS_NEW = "NEW";//刚创建
    const STATUS_RUNNING = "RUNNING";//运行中
    const STATUS_TERMINATED = "TERMINATED";//被终止
    const STATUS_COMPLETED = "COMPLETED";//完成
    const STATUS_CANCELED = "CANCELED";//取消

    //审批实例结果
    const RESULT_AGREE = "agree";
    const RESULT_REFUSE = "refuse";

    //审批实例业务动作
    const BIZ_ACTION_MODIFY = "MODIFY";//表示该审批实例是基于原来的实例修改而来
    const BIZ_ACTION_REVOKE = "REVOKE";//表示该审批实例是由原来的实例撤销后重新发起的
    const BIZ_ACTION_NONE = "NONE";//表示正常发起

    //审批任务状态
    const TASK_STATUS_NEW = "NEW";//未启动
    const TASK_STATUS_RUNNING = "RUNNING";//处理中
    const TASK_STATUS_PAUSED = "PAUSED";//暂停
    const TASK_STATUS_CANCELED = "CANCELED";//取消
    const TASK_STATUS_COMPLETED = "COMPLETED";//完成
    const TASK_STATUS_TERMINATED = "TERMINATED";//终止

    //审批任务结果
    const TASK_RESULT_AGREE = "AGREE";//同意
    const TASK_RESULT_REFUSE = "REFUSE";//拒绝
    const TASK_RESULT_REDIRECTED = "REDIRECTED";//转交

    //操作记录类型
    const OPERATION_RECORD_TYPE_EXECUTE_TASK_NORMAL = "EXECUTE_TASK_NORMAL";//正常执行任务
    const OPERATION_RECORD_TYPE_EXECUTE_TASK_AGENT = "EXECUTE_TASK_AGENT";//代理人执行任务
    const OPERATION_RECORD_TYPE_APPEND_TASK_BEFORE = "APPEND_TASK_BEFORE";//前加签任务
    const OPERATION_RECORD_TYPE_APPEND_TASK_AFTER = "APPEND_TASK_AFTER";//后加签任务
    const OPERATION_RECORD_TYPE_REDIRECT_TASK = "REDIRECT_TASK";//转交任务
    const OPERATION_RECORD_TYPE_START_PROCESS_INSTANCE = "START_PROCESS_INSTANCE";//发起流程实例
    const OPERATION_RECORD_TYPE_TERMINATE_PROCESS_INSTANCE = "TERMINATE_PROCESS_INSTANCE";//终止(撤销)流程实例
    const OPERATION_RECORD_TYPE_FINISH_PROCESS_INSTANCE = "FINISH_PROCESS_INSTANCE";//结束流程实例
    const OPERATION_RECORD_TYPE_ADD_REMARK = "ADD_REMARK";//添加评论

    //操作记录结果
    const OPERATION_RECORD_RESULT_AGREE = "AGREE";//同意
    const OPERATION_RECORD_RESULT_REFUSE = "REFUSE";//拒绝


    public static function _q() {
        return Db::name("enterprise_bpms_instance");
    }

    /**
     * 获取审批实例
     *
     * @param $eid
     * @param $instanceId
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @return array
     */
    public static function get($eid, $instanceId) {
        return self::_q()->where([
            'enterprise_id' => $eid,
            'process_instance_id' => $instanceId,
            'is_deleted' => 0
        ])->find();
    }

    /**
     * 添加或更新审批实例
     *
     * @param $eid
     * @param array $data
     * @return int|string
     */
    public static function addOrUpdate($eid, array $data) {
        $data['enterprise_id'] = $eid;
        $data['is_deleted'] = 0;
        if (!isset($data['gmt_created'])) {
            $data['gmt_created'] = time();
        }
        if (!isset($data['gmt_modified'])) {
            $data['gmt_modified'] = time();
        }

        return self::_q()->insertGetId($data, true);
    }

}